package com.huarui.part.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.part.model.Part;
import com.huarui.part.model.PartUpload;
import com.huarui.part.service.PartUploadService;
import com.huarui.saleReturn.util.ConstantParamEnum;

public class PartUploadManageAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4215043615833960228L;

	private Integer start;

	private Integer limit = 20;

	private Map<String, Object> resultMap;

	private PartUploadService partUploadService;

	private String queryParam;

	public String list() {

		Map<String, Object> map;
		if (queryParam != null) {

			JSONObject queryObject = JSONObject.fromObject(queryParam);

			map = (Map) JSONObject.toBean(queryObject, Map.class);
		} else {

			map = new HashMap<String, Object>();
		}
		List<String> list = new ArrayList<String>();
		if (map.get("status") == null
				|| map.get("status").toString().equals("")) {

			list.add(PartUpload.STATUS_SUBMIT);
			list.add(PartUpload.STATUS_FAILURE);
			list.add(PartUpload.STATUS_SUCCESSED);

		} else {
			list.add(map.get("status").toString());

		}
		map.put("status", list);
		ListModel<PartUpload> lm = new ListModel<PartUpload>();

		lm.setPageSize(limit);

		lm.setStart(start);

		lm = partUploadService.getAllPartUploadListByConditions(lm, map);

		resultMap = new HashMap<String, Object>();

		resultMap.put(ConstantParamEnum.TOTALPROPERTY.getType(), lm.getTotal());

		resultMap.put("page", lm.getPage());

		resultMap.put(ConstantParamEnum.ROOT.getType(), lm.getResult());

		return JSON;
	}

	public String submitToPart() {

		JSONObject queryObject = JSONObject.fromObject(queryParam);

		Part part = (Part) JSONObject.toBean(queryObject, Part.class);

		part.setCode(part.getCode().trim());
		part.setName(part.getName().trim());
		part.setNameEn(part.getNameEn().trim());
		resultMap = partUploadService.submitToPart(part, part.getId());

		return JSON;
	}

	/**
	 * 审核不通过
	 * 
	 * @return
	 */
	public String submitNotPass() {

		JSONObject jo = JSONObject.fromObject(queryParam);

		resultMap = partUploadService.noPass(jo.getInt("id"),
				jo.getString("errorMsg"));

		return JSON;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;

	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public void setPartUploadService(PartUploadService partUploadService) {
		this.partUploadService = partUploadService;
	}

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

}
